package com.moxiao.yfjh.module.blind.controller.admin.distributionsharing;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.blind.controller.admin.distributionsharing.vo.*;
import com.moxiao.yfjh.module.blind.convert.distributionsharing.DistributionSharingConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.distributionsharing.DistributionSharingDO;
import com.moxiao.yfjh.module.blind.service.distributionsharing.DistributionSharingService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 相亲分销分享")
@RestController
@RequestMapping("/blind/distribution-sharing")
@Validated
public class DistributionSharingController {

    @Resource
    private DistributionSharingService distributionSharingService;

    @PostMapping("/create")
    @Operation(summary = "创建相亲分销分享")
    @PreAuthorize("@ss.hasPermission('blind:distribution-sharing:create')")
    public CommonResult<Long> createDistributionSharing(@Valid @RequestBody DistributionSharingCreateReqVO createReqVO) {
        return success(distributionSharingService.createDistributionSharing(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新相亲分销分享")
    @PreAuthorize("@ss.hasPermission('blind:distribution-sharing:update')")
    public CommonResult<Boolean> updateDistributionSharing(@Valid @RequestBody DistributionSharingUpdateReqVO updateReqVO) {
        distributionSharingService.updateDistributionSharing(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲分销分享")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blind:distribution-sharing:delete')")
    public CommonResult<Boolean> deleteDistributionSharing(@RequestParam("id") Long id) {
        distributionSharingService.deleteDistributionSharing(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲分销分享")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blind:distribution-sharing:query')")
    public CommonResult<DistributionSharingRespVO> getDistributionSharing(@RequestParam("id") Long id) {
        DistributionSharingDO distributionSharing = distributionSharingService.getDistributionSharing(id);
        return success(DistributionSharingConvert.INSTANCE.convert(distributionSharing));
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲分销分享列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blind:distribution-sharing:query')")
    public CommonResult<List<DistributionSharingRespVO>> getDistributionSharingList(@RequestParam("ids") Collection<Long> ids) {
        List<DistributionSharingDO> list = distributionSharingService.getDistributionSharingList(ids);
        return success(DistributionSharingConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲分销分享分页")
    @PreAuthorize("@ss.hasPermission('blind:distribution-sharing:query')")
    public CommonResult<PageResult<DistributionSharingRespVO>> getDistributionSharingPage(@Valid DistributionSharingPageReqVO pageVO) {
        PageResult<DistributionSharingDO> pageResult = distributionSharingService.getDistributionSharingPage(pageVO);
        return success(DistributionSharingConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲分销分享 Excel")
    @PreAuthorize("@ss.hasPermission('blind:distribution-sharing:export')")
    @OperateLog(type = EXPORT)
    public void exportDistributionSharingExcel(@Valid DistributionSharingExportReqVO exportReqVO,
                                               HttpServletResponse response) throws IOException {
        List<DistributionSharingDO> list = distributionSharingService.getDistributionSharingList(exportReqVO);
        // 导出 Excel
        List<DistributionSharingExcelVO> datas = DistributionSharingConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲分销分享.xls", "数据", DistributionSharingExcelVO.class, datas);
    }

}
